<?php

class AdminController extends GTL_Action {

    public function init() {
        parent::init();
       
    }

    public function indexAction() {
        $this->_helper->redirector('list', 'admin', '');
    }
  
  
    public function editprofileAction() {

        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $adminData = Array();
        //Load login Model
        $loginModel = new Model_Login();
        //Load Admin Form
        $adminForm = new Form_AdminForm();
        $lgn_id = $this->USER->lgn_id;
        
        if (!empty($lgn_id)) {
            $adminForm->lgn_email->setAttribs(Array('readonly' => true));
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $loginModel->fetchEntryById($lgn_id);
        }


        //Populate form 
        $this->view->form = $adminForm->populate($data);
        if ($this->getRequest()->isPost()) {

            //Check for Errors
            $postedData = $this->_request->getPost();
            
            if (!$adminForm->isValid($postedData)) {
                $errorMessage = $adminForm->getMessages();
                
               
                
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
                if( $postedData['lgn_password']!= '' && $postedData['lgn_confirmpassword'] == '' ){
                     $errorMessageArray[] = "Please enter confirm password.";
                } else if( $postedData['lgn_password']!= $postedData['lgn_confirmpassword']){
                     $errorMessageArray[] = "Please enter password and confirm password same. ";
                }
                if (count($errorMessageArray) > 0) {
                    
                } else {
                    //Admin save
                    $loginData['lgn_id'] = $lgn_id;
                    if($postedData['lgn_password'] != '' ){
                        $loginData['lgn_password'] = md5($postedData['lgn_password']);
                        $lgn_id = $loginModel->save($loginData, 'lgn_id');
                    }
                    
                   
                    $successmessage = 'Profile updated successfully';
                    $this->addSuccessMessage($successmessage);
                   // $this->_helper->redirector('editprofile', 'admin', '');
                }
            }
        }

       
         
            $this->view->adminid = $lgn_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            $this->view->form = $adminForm;
            
       
    }

}

?>
